import { useCartStore } from '@/stores/cart'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { Toast } from 'vant'
export const useCart = () => {
	const store = useCartStore() 
    const router = useRouter()
    // 购物车商品列表
	const cartList = ref(store.cartList) 
    const list = cartList.value
    // 删除商品
    const deleteProduct = ()=>{
        // let id = router.params.id;
        // console.log("商品详情id",id)
        // console.log(list);   
        let idd= list.findIndex((item)=>{
            return item.id
        })
        list.splice(idd-1,1)
        Toast.success('删除成功')
    }
    
    // 提交订单
    const onSubmit = ()=>{
        const newList = ref([])
        list.map((item)=>{
                if(item.state == true){
                    newList.value.push(item)
                }
            })
        list.every((item)=>{
            if(item.state == false){
                Toast.success('您还没有选择商品哦')
            }else{
                router.push('/submit')
            }
        })
            return newList
    }

            
    return { cartList,
            deleteProduct,
            onSubmit,
    }
}
